const app = getApp()
const API = require('../../fetch/index.js');
const http = require('../../http/index');
const APICONSTE = require('../../http/apiConst');

Page({
  data: {
    editUrl: '../../images/add_btn.png',
    avatarUrl: './user-unlogin.png',
    userInfo: {},
    logged: false,
    currentMonthAccount: 0,
    isShowAuthorize: false,
    type: 'pay',
    incomeList: [],
    incomeTotalAccount: 0,
    incomeCurrentMonthAccount: 0,
    accountList: [],
    isLoadingAccount: false,
    isNoMore: false,
    totalAccount: 0
  },

  changeType(e) {
    this.setData({
      type: e.target.dataset.type
    });
    this.init();
  },

  async getIncomeList() {
    var { list, total, currentMonthTotal } = await http({
      action: APICONSTE.QUERY_INCOMES
    });
    this.setData({
      incomeTotalAccount: total,
      incomeCurrentMonthAccount: currentMonthTotal,
      incomeList: list
    });
  },

  async getAllAccout() {
    this.setData({
      isLoadingAccount: true
    });
    var { accountList } = this.data;
    var { list } = await http({
      action: APICONSTE.QUERY_ACCOUNTS,
      data: {
        skipNum: accountList.length
      }
    });
    if (accountList.length == 0) {
      var { total, currentMonthTotal } = await http({
        action: APICONSTE.QUERY_TOTAL_ACCOUNT
      });
      this.setData({
        totalAccount: total,
        currentMonthAccount: currentMonthTotal,
      });
    }
    this.setData({
      accountList: accountList.concat(list),
      isLoadingAccount: false,
      isNoMore: list.length === 0
    });
  },

  // 初始化
  init() {
    var { type } = this.data;
    if (type == 'pay') {
      this.getAllAccout();
    } else {
      this.getIncomeList();
    }
  },

  async onShow() {
    this.setData({
      accountList: [],
      isLoadingAccount: false
    });
    var res = await API.getSetting();
    if (res.authSetting['scope.userInfo']) {
      await this.tologin();
      await this.init();
    }
    var myUserInfo = app.globalData.myUserInfo;
    if (myUserInfo) {
      var avatarUrl = this.data.avatarUrl;
      this.setData({
        logged: true,
        userInfo: myUserInfo,
        avatarUrl: myUserInfo.avatarUrl || avatarUrl
      });
    }
  },

  lookClick(event) {
    var id = event.currentTarget.dataset.id;
    var { type } = this.data;
    wx.navigateTo({
      url: `../accountInfo/accountInfo?id=${id}&type=${type}`
    });
    // wx.requestSubscribeMessage({
    //   tmplIds: ['HjeQIg5dw0WHq7VINKhGL0BDWToX1ElpeWbFcmF8-fo'],
    //   success(res) {
    //     debugger;
    //   }
    // })
  },

  onLoad: function () {
    
  },

  async tologin() {
    var { openid } = await API.login({});
    app.globalData.openid = openid;
    var res = await API.getUserInfo();
    var { userInfo } = res;
    this.setData({
      logged: true,
      avatarUrl: userInfo.avatarUrl,
      userInfo: userInfo
    });
    app.globalData.userInfo = userInfo;
  },

  // 跳转记账页面
  addAccount() {
    if (!app.globalData.openid) {
      this.setData({
        isShowAuthorize: true
      });
    } else {
      var { type } = this.data;
      wx.navigateTo({
        url: `../addAccount/addAccount?type=${type}`
      });
    }
  },

  // 获取登录权限
  async onGetUserInfo(e) {
    var userInfo = e.detail;
    if (userInfo) {
      this.setData({
        logged: true,
        avatarUrl: userInfo.avatarUrl,
        userInfo: userInfo
      });
      app.globalData.myUserInfo = userInfo;
      this.setData({
        isShowAuthorize: false
      });
    }
    await this.tologin();
    wx.navigateTo({
      url: '../addAccount/addAccount'
    });
  },

  onReachBottom() {
    this.setData({
      isLoadingAccount: true
    });
    this.init();
  },

  async onPullDownRefresh() {
    this.setData({
      accountList: [],
      isLoadingAccount: false
    });
    await this.init();
    wx.stopPullDownRefresh();
  }

})
